<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Gpeople extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'), $this->session->userdata('permission_verify'));
        $this->data['controller'] = 'gpeople';
        $this->load->model('Mgpeople');
        $this->_per = $this->checkPerMission($this->session->userdata('admin'), $this->session->userdata('SessionPermisson'), 5, $this->session->userdata('admin_id'));
    }    
    
    public function incentives()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mgpeople->dbincentives($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
        $this->data['action'] = 'incentives';
        $this->load->view('template/layout', $this->data);
    }
    
    public function addincentives()
    {
        if($this->_per->add != 2)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->data['arrRestaurant'] = $this->Mgpeople->dbgetrestaurant();
        $this->data['month'] = $this->select_month();
        $this->data['year'] = $this->select_year();
        
        $this->data['ecoupon'] = $this->Mgpeople->selecteCoupon();
        $this->data['province'] = $this->Mgpeople->listProvince();
        /*control*/
        $this->load->library('form_validation');
        $type = $this->getParamInt('incentives_type');
        if($type == 1){
            $this->form_validation->set_rules('sl_month', 'Tháng', 'required');
            $this->form_validation->set_rules('sl_year', 'Năm', 'required');
        }
        $type_sl = $this->getParamInt('restaurant_target');
        if($type_sl == 1){
            $this->form_validation->set_rules('restaurants', 'Nhà hàng', 'required');
        }
        $province_apply = $this->getParamInt('province_apply');
        if($province_apply == 1){
            $this->form_validation->set_rules('province', 'Tỉnh thành phố áp dụng', 'required');
        }
        
        $this->form_validation->set_rules('title', 'Tên ưu đãi', 'required');
        $this->form_validation->set_rules('img_incens_hd_', 'Ảnh ưu đãi', 'required');
        $this->form_validation->set_rules('img1_incens_hd_', 'Ảnh bài viết', 'required');
        $this->form_validation->set_rules('summary', 'Mô tả', 'required');
        $this->form_validation->set_rules('content', 'Nội dung', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $title = $this->getParamString('title');
            
			/*
			$dataimage = $this->getParamString('img_incens_hd_');
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'incentives-'.$timenow.'.jpg';
                $path = 'images/incentives/'.date('Ym').'/';
                @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
                $paths = PROJECT_PATH.'/data/'.$path;
                $arr_exp = array('[removed]', '[removed');
                $exp = str_replace($arr_exp,'',$dataimage);
                $this->savefile($exp,$paths,$nameimagess);
                $img = $path.$nameimagess;
            }else{
                $img = '';
            }
            
            /* Ảnh bài viết
            $dataimage1 = $this->getParamString('img1_incens_hd_');
            if(!in_array($dataimage1, array(null, '', '0')))
            {
                $timenow1 = time().rand(0,9999);
                $nameimagess1 = 'incentives-'.$timenow1.'.jpg';
                $path1 = 'images/incentives/'.date('Ym').'/';
                @mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
                $paths1 = PROJECT_PATH.'/data/'.$path1;
                $arr_exp1 = array('[removed]', '[removed');
                $exp1 = str_replace($arr_exp1,'',$dataimage1);
                $this->savefile($exp1,$paths1,$nameimagess1);
                $img1 = $path1.$nameimagess1;
            }else{
                $img1 = '';
            }
            */
			
			$path1 = 'images/incentives/';
			@mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
			$config['upload_path'] = '../data/'.$path1;
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2000';
			$config['max_width']  = '10240';
			$config['max_height']  = '7680';
			$config['encrypt_name'] =  TRUE;// TRUE/FALSE (boolean)
			$this->load->library('upload', $config);
			if( !$this->upload->do_upload('fileupload')){
				$img = '';
			}else{
				$data_img = $this->upload->data();
				$img = $path1.$data_img['file_name'];
			}		
			if( !$this->upload->do_upload('fileupload1')){
				$img1 = '';
			}else{
				$data_img1 = $this->upload->data();
				$img1 = $path1.$data_img1['file_name'];
			}
			
			
            $summary = $this->getParamStringAdmin('summary');
            $content = $this->getParamStringAdmin('content');
            $ecoupon = $this->getParamInt('sl_coupon');
			$orderby = $this->getParamInt('orderby');

            
            if($type == 1){
                $month = $this->getParamInt('sl_month');
                $year = $this->getParamInt('sl_year');
            }else{
                $month = $year = 0;
            }
            if($type_sl == 1){
                $restaurant = $this->getParamArray('restaurants');
            }else{
                $restaurant = 0;
            }
            if($province_apply == 1){
                $province = $this->getParamArray('province');
            }else{
                $province = 0;
            }
            
            $this->data['return'] = $this->Mgpeople->dbaddincentives($title,$img,$summary,$content,$type,$month,$year,$this->session->userdata('admin_id'),$type_sl,$restaurant?$restaurant:0,$img1,$ecoupon,$province_apply,$province,$orderby);
            if($this->data['return'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/g-people/add-incentives";</script>');
            }
        }
         
        $this->data['action'] = 'addincentives';
        $this->load->view('template/layout', $this->data);
    }
    
    private function select_month()
    {
        $data = array(
            '1' => 'Tháng 1',
            '2' => 'Tháng 2',
            '3' => 'Tháng 3',
            '4' => 'Tháng 4',
            '5' => 'Tháng 5',
            '6' => 'Tháng 6',
            '7' => 'Tháng 7',
            '8' => 'Tháng 8',
            '9' => 'Tháng 9',
            '10' => 'Tháng 10',
            '11' => 'Tháng 11',
            '12' => 'Tháng 12',
        );
        return $data;
    }
    private function select_year()
    {
        $start = 2013;
        $data = array();
        for($start;$start <= 2100;$start++)
        {
            $data[$start] = 'Năm '.$start;   
        }
        return $data;
    }
    
    public function editincentives()
    {
        if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->data['arrRestaurant'] = $this->Mgpeople->dbgetrestaurant();
       
        $this->data['month'] = $this->select_month();
        $this->data['year'] = $this->select_year();
        
        $this->data['ecoupon'] = $this->Mgpeople->selecteCoupon();
        $this->data['province'] = $this->Mgpeople->listProvince();
        
        $this->data['id'] = $this->security->xss_clean($this->getParamUri(4,1));
        $this->data['data'] = $this->Mgpeople->dbgetinfoincentives($this->data['id']);
        $this->data['return'] = 0;
        
        /*control*/
        $this->load->library('form_validation');
        $type = $this->getParamInt('incentives_type');
        if($type == 1){
            $this->form_validation->set_rules('sl_month', 'Tháng', 'required');
            $this->form_validation->set_rules('sl_year', 'Năm', 'required');
        }
        $type_sl = $this->getParamInt('restaurant_target');
        if($type_sl == 1){
            $this->form_validation->set_rules('restaurants', 'Nhà hàng', 'required');
        }
        $province_apply = $this->getParamInt('province_apply');
        if($province_apply == 1){
            $this->form_validation->set_rules('province', 'Tỉnh thành phố áp dụng', 'required');
        }
        
        
        $this->form_validation->set_rules('title', 'Tên ưu đãi', 'required');
        $this->form_validation->set_rules('img_incens_hd_check', 'Ảnh ưu đãi', 'required');
        $this->form_validation->set_rules('img1_incens_hd_check', 'Ảnh bài viết', 'required');
        $this->form_validation->set_rules('summary', 'Mô tả', 'required');
        $this->form_validation->set_rules('content', 'Nội dung', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $title = $this->getParamString('title');
            $dataimage = $this->getParamString('img_incens_hd_');
            
            $dataimage_check = $this->getParamString('img_incens_hd_check');
            $verify_image = $this->getParamString('img_incens_verify');
            if(!in_array($verify_image, array(null, '')))
            {
                if($verify_image != $dataimage_check)
                {
                    @unlink(PROJECT_PATH.'/data/'.$verify_image);
                }
            }
            
            /*if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'incentives-'.$timenow.'.jpg';
                $path = 'images/incentives/'.date('Ym').'/';
                @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
                $paths = PROJECT_PATH.'/data/'.$path;
                $arr_exp = array('[removed]', '[removed');
                $exp = str_replace($arr_exp,'',$dataimage);
                $this->savefile($exp,$paths,$nameimagess);
                $img = $path.$nameimagess;
            }else{
                $img = '';
            }*/
            
            /* Ảnh bài viết */
            $dataimage1 = $this->getParamString('img1_incens_hd_');
            
            $dataimage_check1 = $this->getParamString('img1_incens_hd_check');
            $verify_image1 = $this->getParamString('img1_incens_verify');
            if(!in_array($verify_image1, array(null, '')))
            {
                if($verify_image1 != $dataimage_check1)
                {
                    @unlink(PROJECT_PATH.'/data/'.$verify_image1);
                }
            }
            
            /*if(!in_array($dataimage1, array(null, '', '0')))
            {
                $timenow1 = time().rand(0,9999);
                $nameimagess1 = 'incentives-'.$timenow1.'.jpg';
                $path1 = 'images/incentives/'.date('Ym').'/';
                @mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
                $paths1 = PROJECT_PATH.'/data/'.$path1;
                $arr_exp1 = array('[removed]', '[removed');
                $exp1 = str_replace($arr_exp1,'',$dataimage1);
                $this->savefile($exp1,$paths1,$nameimagess1);
                $img1 = $path1.$nameimagess1;
            }else{
                $img1 = '';
            }*/
            
            $path1 = 'images/incentives/';
			@mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
			$config['upload_path'] = '../data/'.$path1;
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '2000';
			$config['max_width']  = '10240';
			$config['max_height']  = '7680';
			$config['encrypt_name'] =  TRUE;// TRUE/FALSE (boolean)
			$this->load->library('upload', $config);
			if( !$this->upload->do_upload('fileupload')){
				$img = '';
			}else{
				$data_img = $this->upload->data();
				$img = $path1.$data_img['file_name'];
			}		
			if( !$this->upload->do_upload('fileupload1')){
				$img1 = '';
			}else{
				$data_img1 = $this->upload->data();
				$img1 = $path1.$data_img1['file_name'];
			}
            
            $summary = $this->getParamStringAdmin('summary');
            $content = $this->getParamStringAdmin('content');
            $ecoupon = $this->getParamInt('sl_coupon');
			$orderby = $this->getParamInt('orderby');
			
            if($type == 1){
                $month = $this->getParamInt('sl_month');
                $year = $this->getParamInt('sl_year');
            }else{
                $month = $year = 0;
            }
            if($type_sl == 1){
                $restaurant = $this->getParamArray('restaurants');
            }else{
                $restaurant = 0;
            }
            if($province_apply == 1){
                $province = $this->getParamArray('province');
            }else{
                $province = 0;
            }
            
            $ids = $this->data['id'];
            $this->data['return'] = $this->Mgpeople->dbeditincentives($ids,$title,$img,$summary,$content,$type,$month,$year,$this->session->userdata('admin_id'),$type_sl,$restaurant?$restaurant:0,$img1,$ecoupon,$province_apply,$province,$orderby);
            if($this->data['return'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/g-people/edit-incentives/'.$ids.'";</script>');
            }
        }
        
        $this->data['action'] = 'editincentives';
        $this->load->view('template/layout', $this->data);
    }
    
    public function activeincentives()
    {
        if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $id = $this->getParamInt('id');
        die(json_encode($this->Mgpeople->dbactiveincentives($id)));
    }
    
    public function deleteincentives()
    {
        if($this->_per->del != 4)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $id = $this->getParamInt('id');
        die(json_encode($this->Mgpeople->dbdeleteincentives($id)));
    }
    
    
    public function deleteimagesajax()
    {
        if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $path = $this->getParamString('path');
        if($path != 1)
        {
            @unlink(PROJECT_PATH.'/data/'.$path);
        }
    }
}
